







function toBusinessEditPanel() {
    var userId = $("#userAuthorityMgr_userIId").val();
    var projectTree =  $.fn.zTree.getZTreeObj("projectSel_tree");
    projectTree.expandAll(true);
    var userProjectArray=[];
    $.ajax({
        url:basePath+"/user/listUsersProjectsSections",
        data:{
            userIds:userId
        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){
            if(data.data.length>0){
                projectTree.checkAllNodes(false);
                var content="";
                $(data.data).each(function () {
                    userProjectArray.push(this.projectId);
                    content += "<dl id='authorProject_"+this.projectId+"'>" +
                        "<dt><span>"+this.projectName+"</span><input type='hidden' name='authorSelProject' value='"+this.projectId+"'/><a class=\"authority_edit_btn\" href=\"javascript:void(0)\" onclick='editAuthorSection(this,\""+userId+"\",\""+this.projectName+"\",\""+this.projectId+"\")'>编辑标段</a></dt>" +
                        "<dd>";
                    if(this.sectionIds&&this.sectionIds != "null"){
                        var sectionIdsArr = this.sectionIds.split(",");
                        var sectionNameArr = this.sectionNames.split(",");
                        for (var i=0;i<sectionNameArr.length;i++){
                            content +="<span>"+sectionNameArr[i]+"<input type='hidden' name='authorSelSection' value='"+sectionIdsArr[i]+"'/></span>";
                        }
                    }
                    content +=     "</dd>" +
                        "</dl>";
                    authorRightShow(1,this.orgId,this.orgName);
                });
                $("#section_content").html(content);

                for (var i=0;i<userProjectArray.length;i++) {
                    var checkNode=projectTree.getNodeByParam("id",userProjectArray[i], null);

                    projectTree.checkNode(checkNode,true,true);
                }
            }else{
                projectTree.checkAllNodes(false);
                $("#section_content").html("");
                $("#user_businessAuthority_box").css("width","460px");
                userAuthorityMgrPanel.find(".selBids_box").hide();
                userAuthorityMgrPanel.find(".selProject_box").addClass("indexAuth_box");

            }

        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            //    $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }

    });

}

















function businessOnCheck(dom,id,treeNode) {
    var treeObj=$.fn.zTree.getZTreeObj("projectSel_tree"),
        nodes=treeObj.getCheckedNodes(true),
        v="";
    var userId = $("#userAuthorityMgr_userIId").val();
    for(var i=0;i<nodes.length;i++){
        if(i == nodes.length-1){
            v+=nodes[i].name
        }else{
            v+=nodes[i].name + ",";
        }
        // alert(nodes[i].id); //获取选中节点的值
    }
    // alert(v);
    if(treeNode.checked == true){
        var content = "<dl id='authorProject_"+treeNode.id+"'>" +
            "<dt><span>"+treeNode.name+"</span><input type='hidden' name='authorSelProject' value='"+treeNode.id+"'/><a class=\"authority_edit_btn\" href=\"javascript:void(0)\" onclick='editAuthorSection(this,\""+userId+"\",\""+treeNode.name+"\",\""+treeNode.id+"\")'>编辑标段</a></dt>" +
            "<dd></dd>" +
            "</dl>";
        $("#section_content").append($(content));
        authorRightShow(1,treeNode.id,treeNode.name);
    }else{
        $("#authorProject_"+treeNode.id).remove();
        if($("#selAuthority_content").find("dl").size() == 0 ){
            $("#user_systemAuthority_box").find(".selOrg_box").addClass("indexAuth_box");
            $("#user_systemAuthority_box").css("width","460px");
            $("#user_systemAuthority_box").find (".selRole_box").removeClass("indexAuth_box");
            $("#user_systemAuthority_box").find (".selRole_box").hide();

        }
    }




}


function sectionOnCheck() {

}

function editAuthorSection(e,userId,projectName,projectId) {
    $("#authorSection_tit").html(projectName);
    $("#authorSection_tit").attr("title",projectName);
    $("#checkAuthorityAllSectionBtn").attr("class","checkAllBtn uncheckBtn");
    $("#sectionAllCheck").prop("checked",false);
    //生成标段树
    var sectionZTreeSetting = {
        data : {
            simpleData : {
                enable: true



            }
        },
        check: {
            enable: true,
            chkStyle:"checkbox",
            chkboxType: { "Y": "", "N": "" }
        },
        callback : {
            onCheck:sectionOnCheck,
            onClick: function (e, treeId, treeNode, clickFlag) {
                var treeObj=$.fn.zTree.getZTreeObj("authorSection_tree");
                treeObj.checkNode(treeNode, !treeNode.checked, true);
            }
        }
    };
    $.ajax({
        url:basePath+"/section/getSectionByProjectId",
        data:{
            projectId:projectId
        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){
            data = data.data;
            var projectSection = new Array();
            for (var i=0;i<data.length;i++){
                projectSection[i] = new Object();
                projectSection[i].id = data[i].sectionId;
                projectSection[i].name = data[i].sectionName;
            }
            $.fn.zTree.init($("#authorSection_tree"),sectionZTreeSetting,projectSection);
            if(data.length>0){



                $.ajax({
                    url:basePath+"/user/listUsersProjectsSections",
                    data:{
                        projectIds:projectId,
                        userIds:userId
                    },
                    dataType:"json",
                    type: "post",
                    contentType:"application/x-www-form-urlencoded",
                    beforeSend:null,
                    success:function(data){
                        data = data.data;
                        if(data.length>0){
                            var userSections = data[0].sectionIds.split(",");
                            var treeObj=$.fn.zTree.getZTreeObj("authorSection_tree");
                            for (var i=0;i<userSections.length;i++) {
                                var checkNode=treeObj.getNodeByParam("id",userSections[i], null);
                                treeObj.checkNode(checkNode,true,true);
                            }
                        }

                    },
                    error:function (XMLHttpRequest, textStatus, errorThrown) {
                        //    $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
                    }
                });
            }else{
                var treeObj=$.fn.zTree.getZTreeObj("authorSection_tree");
                treeObj.checkAllNodes(false);
            }


            $("#selAuthorty_SelProject").val(projectId);
            $("#authorSelSection_box").show();
            $("#authorityBusiness_unsubmit").show();
            $("#authorityBusiness_submit").hide();

        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            //    $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    });
    






}

function toEditUserProjectSection(){
    var zTreeObj = $.fn.zTree.getZTreeObj("authorSection_tree");
    var checkedNodes = zTreeObj.getCheckedNodes();
    var projectId = $("#selAuthorty_SelProject").val();
    var content = "";
    for(var i=0; i<checkedNodes.length;i++){
        content += "<span>"+checkedNodes[i].name+"<input type='hidden' name='authorSelSection' value='"+checkedNodes[i].id+"'/></span>"
    }
    $("#authorProject_"+projectId).find("dd").html(content);
    $("#authorSelSection_box").hide();
    $("#authorityBusiness_unsubmit").hide();
    $("#authorityBusiness_submit").show();
}



function toSubProjectSection(){
    var userId = $("#userAuthorityMgr_userIId").val();
    var projectLength = $("#section_content").find("dl").size();
    var strArr = new Array();
    for(var i=0;i<projectLength;i++ ){
        var projectId = $("#section_content").find("dl").eq(i).find("input[name=authorSelProject]").val();
        if($("#section_content").find("dl").eq(i).find("dd").find("span").size()>0){
            $("#section_content").find("dl").eq(i).find("dd").find("span").each(function (j) {
                var obj = {
                    projectId:projectId,
                    sectionId:$(this).find("input[name=authorSelSection]").val()
                };
                strArr.push(obj);
            })
        }else{
            var obj = {
                projectId:projectId
            };
            strArr.push(obj);
        }

    }
    var strJson = JSON.stringify(strArr);
    $.wdfProgress.progress("正在请求...", $("body"));
    $.ajax({
        url:basePath+"/user/changUserProjectsSections",
        data:{
            userId:userId,
            userProjectsSectionsStr:strJson
        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){
            if(data.status){
                $.wdfProgress.showMsg(data.msg, $("body"));
                $.wdfProgress.progRemove($("body"), function() {
                }, 1666);
            }


        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    })
}


function businessAllOnCheck(treeNode) {
    var treeObj=$.fn.zTree.getZTreeObj("projectSel_tree"),
        nodes=treeObj.getCheckedNodes(true),
        v="";
    var userId = $("#userAuthorityMgr_userIId").val();
    for(var i=0;i<nodes.length;i++){
        if(i == nodes.length-1){
            v+=nodes[i].name
        }else{
            v+=nodes[i].name + ",";
        }
        // alert(nodes[i].id); //获取选中节点的值
    }
    // alert(v);
    if(treeNode.checked == true){
        var content = "<dl id='authorProject_"+treeNode.id+"'>" +
            "<dt><span>"+treeNode.name+"</span><input type='hidden' name='authorSelProject' value='"+treeNode.id+"'/><a class=\"authority_edit_btn\" href=\"javascript:void(0)\" onclick='editAuthorSection(this,\""+userId+"\",\""+treeNode.name+"\",\""+treeNode.id+"\")'>编辑标段</a></dt>" +
            "<dd></dd>" +
            "</dl>";
        $("#section_content").append($(content));
        authorRightShow(1,treeNode.id,treeNode.name);
    }else{
        $("#authorProject_"+treeNode.id).remove();
        if($("#selAuthority_content").find("dl").size() == 0 ){
            $("#user_systemAuthority_box").find(".selOrg_box").addClass("indexAuth_box");
            $("#user_systemAuthority_box").css("width","460px");
            $("#user_systemAuthority_box").find (".selRole_box").removeClass("indexAuth_box");
            $("#user_systemAuthority_box").find (".selRole_box").hide();

        }
    }




}


function checkAuthorityAllProject(e){
    var projectTree =  $.fn.zTree.getZTreeObj("projectSel_tree");
    if($(e).hasClass("uncheckBtn")){
        $(e).removeClass("uncheckBtn");
        $(e).addClass("checkedBtn");
        $(e).prev("input[type=checkbox]").prop("checked",true);
        $("#section_content").html("");
        projectTree.checkAllNodes(true);
        var nodes=projectTree.getCheckedNodes(true);
        for (var i=0; i<nodes.length;i++){
            businessAllOnCheck(nodes[i]);
        }
    }else{
        $("#section_content").html("");
        $(e).removeClass("checkedBtn");
        $(e).addClass("uncheckBtn");
        $(e).prev("input[type=checkbox]").prop("checked",false);
        projectTree.checkAllNodes(false);
        $("#user_systemAuthority_box").find(".selProject_box").addClass("indexAuth_box");
        $("#user_systemAuthority_box").css("width","460px");
        $("#user_systemAuthority_box").find (".selBids_box").removeClass("indexAuth_box");
        $("#user_systemAuthority_box").find (".selBids_box").hide();
    }
}
function checkAuthorityAllSection(e){
    var zTreeObj = $.fn.zTree.getZTreeObj("authorSection_tree");
    if($(e).hasClass("uncheckBtn")){
        $(e).removeClass("uncheckBtn");
        $(e).addClass("checkedBtn");
        $(e).prev("input[type=checkbox]").prop("checked",true);
        zTreeObj.checkAllNodes(true);

    }else{

        $(e).removeClass("checkedBtn");
        $(e).addClass("uncheckBtn");
        $(e).prev("input[type=checkbox]").prop("checked",false);
        zTreeObj.checkAllNodes(false);

    }
}




$(function () {
    $(document).click(function () {
        $("#authorSelSection_box").hide();
        $("#authorityBusiness_unsubmit").hide();
        $("#authorityBusiness_submit").show();
    })
    $("#authorSelSection_box").click(function (e) {
        stopPropagation(e);
    })
    $("#projectAllCheck").change(function () {
        var projectTree =  $.fn.zTree.getZTreeObj("projectSel_tree");
        if($(this).prop("checked") == true){
            $("#section_content").html("");
            projectTree.checkAllNodes(true);
            var nodes=projectTree.getCheckedNodes(true);
            for (var i=0; i<nodes.length;i++){
                businessAllOnCheck(nodes[i]);
            }

        }else{
            projectTree.checkAllNodes(false);
            $("#section_content").html("");
            projectTree.checkAllNodes(false);
            $("#user_businessAuthority_box").find(".selProject_box").addClass("indexAuth_box");
            $("#user_businessAuthority_box").css("width","460px");
            $("#user_businessAuthority_box").find (".selBids_box").removeClass("indexAuth_box");
            $("#user_businessAuthority_box").find (".selBids_box").hide();
        }
    })
    $("#sectionAllCheck").change(function () {
        var zTreeObj = $.fn.zTree.getZTreeObj("authorSection_tree");
        if($(this).prop("checked") == true){

            zTreeObj.checkAllNodes(true);
        }else{
            zTreeObj.checkAllNodes(false);
        }

    })
})